//取消当前商品活动
function showCancel(goodsId) {
    if (confirm("确认取消当前商品活动？")) {
        $.ajax({
            type: 'get',
            url: 'activity/stop',
            dataType: 'json',
            data: {
                "goodsId": goodsId
            },
            error: function (request) {
                alert("未知错误，取消活动失败");
            },
            success: function (res) {
                if (res.code == 0) {
                    alert("取消商品活动成功！");
                    window.location.reload();
                } else {
                    alert(res.msg);
                }
            }
        });
    }
}


//点击设置拍卖活动
function showAuction(goodsId) {
    $.ajax({
        url : "auction/test",
        async : true,
        type : "GET",
        dataType : "json",
        //成功后开启模态框
        success : queryAuction,
        error : function(res) {
            alert("网络错误");
        }
    });

    function queryAuction(res) {
        if(res.code!=0){
            alert(res.msg);
            return;
        }
        // 显示模态框
        $("#a_goodsId").val(goodsId);
        $('#auctionModal').modal('show');
    }
}
//提交拍卖活动
$("#auctionButton").click(function () {
    var data = new FormData(document.getElementById("auctionForm"));
    // 判断是否为空
    if(data.get('goodsId')===null||data.get('goodsId')===undefined||data.get('goodsId')==''||
        data.get('auctionOriginPrice')===null||data.get('auctionOriginPrice')===undefined||data.get('auctionOriginPrice')==''||
        data.get('auctionBottomPrice')===null||data.get('auctionBottomPrice')===undefined||data.get('auctionBottomPrice')==''||
        data.get('auctionPricespan')===null||data.get('auctionPricespan')===undefined||data.get('auctionPricespan')==''||
        data.get('auctionBeginTime')===null||data.get('auctionBeginTime')===undefined||data.get('auctionBeginTime')==''||
        data.get('auctionEndTime')===null||data.get('auctionEndTime')===undefined||data.get('auctionEndTime')==''||
        data.get('auctionTimespan')===null||data.get('auctionTimespan')===undefined||data.get('auctionTimespan')==''){
        alert("数据不能为空，请检查。");
        return;
    }
    // 判断金额格式
    var auctionOriginPrice = data.get('auctionOriginPrice');
    var auctionBottomPrice = data.get('auctionBottomPrice');
    var auctionPricespan = data.get('auctionPricespan');
    var moneyReg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
    if(!moneyReg.test(auctionOriginPrice)||!moneyReg.test(auctionBottomPrice)||!moneyReg.test(auctionPricespan)){
        alert('请输入正确的金额！');
        return;
    }
    $.ajax({
        type: 'post',
        url: 'auction/set',
        dataType: 'json',
        data: data,
        contentType: false, //不设置内容类型
        processData: false, //不处理数据
        error: function(request) {
            alert("未知错误，请检查输入格式！");
        },
        success: function(res) {
            if(res.code==0) {
                alert("设置新一期拍卖成功！");
                window.location.reload();
            }else{
                alert(res.msg);
            }
        }
    });
});


//点击设置秒杀活动
function showSeckill(goodsId) {
    $.ajax({
        url : "seckill/test",
        async : true,
        type : "GET",
        dataType : "json",
        //成功后开启模态框
        success : queryAuction,
        error : function(res) {
            alert("网络错误");
        }
    });

    function queryAuction(res) {
        if(res.code!=0){
            alert(res.msg);
            return;
        }
        // 显示模态框
        $("#s_goodsId").val(goodsId);
        $('#seckillModal').modal('show');
    }
}
//提交秒杀活动
$("#seckillButton").click(function () {
    var data = new FormData(document.getElementById("seckillForm"));
    // 判断是否为空
    if(data.get('goodsId')===null||data.get('goodsId')===undefined||data.get('goodsId')==''||
        data.get('seckillPrice')===null||data.get('seckillPrice')===undefined||data.get('seckillPrice')==''||
        data.get('seckillCount')===null||data.get('seckillCount')===undefined||data.get('seckillCount')==''||
        data.get('seckillBeginTime')===null||data.get('seckillBeginTime')===undefined||data.get('seckillBeginTime')==''){
        alert("数据不能为空，请检查。");
        return;
    }
    // 判断金额格式
    var seckillPrice = data.get('seckillPrice');
    var moneyReg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
    if(!moneyReg.test(seckillPrice)){
        alert('请输入正确的金额！');
        return;
    }
    $.ajax({
        type: 'post',
        url: 'seckill/set',
        dataType: 'json',
        data: data,
        contentType: false, //不设置内容类型
        processData: false, //不处理数据
        error: function(request) {
            alert("未知错误，请检查输入格式！");
        },
        success: function(res) {
            if(res.code==0) {
                alert("设置新一期秒杀成功！");
                window.location.reload();
            }else{
                alert(res.msg);
            }
        }
    });
});



//点击设置今日特价活动
function showOnsale(goodsId) {
    // 显示模态框
    $("#o_goodsId").val(goodsId);
    $('#onsaleModal').modal('show');
}
//提交今日特价活动
$("#onsaleButton").click(function () {
    var data = new FormData(document.getElementById("onsaleForm"));
    // 判断是否为空
    if(data.get('goodsId')===null||data.get('goodsId')===undefined||data.get('goodsId')==''||
        data.get('onsalePrice')===null||data.get('onsalePrice')===undefined||data.get('onsalePrice')==''){
        alert("数据不能为空，请检查。");
        return;
    }
    // 判断金额格式
    var onsalePrice = data.get('onsalePrice');
    var moneyReg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
    if(!moneyReg.test(onsalePrice)){
        alert('请输入正确的金额！');
        return;
    }
    $.ajax({
        type: 'post',
        url: 'onsale/set',
        dataType: 'json',
        data: data,
        contentType: false, //不设置内容类型
        processData: false, //不处理数据
        error: function(request) {
            alert("未知错误，请检查输入格式！");
        },
        success: function(res) {
            if(res.code==0) {
                alert("添加今日特价商品成功！");
                window.location.reload();
            }else{
                alert(res.msg);
            }
        }
    });
});